package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanServiceImpl nursingProjectPlanService;


    /**
     * 查询护理计划 day0217 查询
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        //1.根据id查询护理计划信息
        NursingPlan nursingPlan = getById(id);
        NursingPlanVo planVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, planVo);
        //2.根据ID查询关联的护理项目计划信息
        List<NursingProjectPlan> list = nursingProjectPlanService.lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id)
                .list();
        //3.将nursingProjectPlanList集合转成nursingProjectPlanVoList集合
        List<NursingProjectPlanVo> collect = list.stream().map(S -> {
            NursingProjectPlanVo P = new NursingProjectPlanVo();
            BeanUtils.copyProperties(S, P);
            P.setProjectId(String.valueOf(S.getProjectId()));
            P.setExecuteCycle(String.valueOf(S.getExecuteCycle()));
            return P;
        }).collect(Collectors.toList());
        planVo.setProjectPlans(collect);
        return planVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划  day0217 新增
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        //1.保存护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        //2.拷贝属性
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        //3.保存项目计划信息
        save(nursingPlan);

        //根据计划ID批量保存项目计划信息&关联的护理项目信息
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        int result = 0;
        if (ObjectUtils.isNotEmpty(projectPlans)) {
            projectPlans.forEach(t -> {
                t.setPlanId(nursingPlan.getId());
            });
            boolean b = nursingProjectPlanService.saveBatch(projectPlans);
            result = b ? 1 : 0;
        }
        return result;
    }

    /**
     * 修改护理计划  day0217 修改
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        // 1、修改护理计划关联的护理项目列表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (ObjectUtils.isNotEmpty(projectPlans)) {
            // 1.1、删除护理计划关联的护理项目列表
            nursingProjectPlanService.lambdaUpdate()
                    .eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId())
                    .remove();
            // 1.2、批量保存护理项目列表
            projectPlans.forEach(t -> {
                t.setPlanId(nursingPlanDto.getId());
            });
            boolean b = nursingProjectPlanService.saveBatch(projectPlans);
        }
        NursingPlan nursingPlan = new NursingPlan();
        return updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }
}
